home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 363 / xlisp20 / xlisplsp / objct1.lsp < prev    next >
Text File  |  1990-02-03  |  3KB  |  69 lines

  1. ; This is an example using the object-oriented programming support in
  2. ; XLISP.  The example involves defining a class of objects representing
  3. ; dictionaries.  Each instance of this class will be a dictionary in
  4. ; which names and values can be stored.  There will also be a facility
  5. ; for finding the values associated with names after they have been
  6. ; stored.
  7.  
  8. ; Create the 'Dictionary' class.
  9.  
  10. (setq Dictionary (Class 'new))
  11.  
  12. ; Establish the instance variables for the new class.
  13. ; The variable 'entries' will point to an association list representing the
  14. ; entries in the dictionary instance.
  15.  
  16. (Dictionary 'ivars '(entries))
  17.  
  18. ; Setup the method for the 'isnew' initialization message.
  19. ; This message will be send whenever a new instance of the 'Dictionary'
  20. ; class is created.  Its purpose is to allow the new instance to be
  21. ; initialized before any other messages are sent to it.  It sets the value
  22. ; of 'entries' to nil to indicate that the dictionary is empty.
  23.  
  24. (Dictionary 'answer 'isnew '()
  25.         '((setq entries nil)
  26.           self))
  27.  
  28. ; Define the message 'add' to make a new entry in the dictionary.  This
  29. ; message takes two arguments.  The argument 'name' specifies the name
  30. ; of the new entry; the argument 'value' specifies the value to be
  31. ; associated with that name.
  32.  
  33. (Dictionary 'answer 'add '(name value)
  34.         '((setq entries
  35.                 (cons (cons name value) entries))
  36.           value))
  37.  
  38. ; Create an instance of the 'Dictionary' class.  This instance is an empty
  39. ; dictionary to which words may be added.
  40.  
  41. (setq d (Dictionary 'new))
  42.  
  43. ; Add some entries to the new dictionary.
  44.  
  45. (d 'add 'mozart 'composer)
  46. (d 'add 'winston 'computer-scientist)
  47. (d 'add '520ST 'powerful-micro)
  48.  
  49. ; Define a message to find entries in a dictionary.  This message takes
  50. ; one argument 'name' which specifies the name of the entry for which to
  51. ; search.  It returns the value associated with the entry if one is
  52. ; present in the dictionary.  Otherwise, it returns nil.
  53.  
  54. (Dictionary 'answer 'find '(name &aux entry)
  55.         '((cond ((setq entry (assoc name entries))
  56.           (cdr entry))
  57.          (t
  58.           nil))))
  59.  
  60. ; Try to find some entries in the dictionary we created.
  61.  
  62. ; (d 'find 'mozart)
  63. ; (d 'find 'winston)
  64. ; (d 'find 'bozo)
  65.  
  66. ; The names 'mozart' and 'winston' are found in the dictionary so their
  67. ; values 'composer' and 'computer-scientist' are returned.  The name 'bozo'
  68. ; is not found so nil is returned in this case.
  69. əəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəə